package com.easyhouse.service;

import com.easyhouse.common.pojo.Message;
import com.easyhouse.common.utils.UUIDUtil;
import com.easyhouse.common.vo.EasyUIResult;
import com.easyhouse.mapper.MessageMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService{
    @Autowired(required = false)
    private MessageMapper messageMapper;

    @Override
    public void insertMessage(Message message) {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String createTime = dateFormat.format(now);//格式化然后放入字符串中
        message.setMessageDate(createTime);
        String messageId = UUIDUtil.getUUID();
        message.setMessageId(messageId);
        String ticket = message.getUserName();
        String userName = StringUtils.substring(ticket,23,ticket.length());
        message.setUserName(userName);
        messageMapper.insertMessage(message);
    }

    @Override
    public EasyUIResult selectMessage(String houseId) {
        EasyUIResult result=new EasyUIResult();
        int total = messageMapper.selectCount(houseId);
        result.setTotal(total);
        List<Message> messages = messageMapper.selectMessage(houseId);
        result.setRows(messages);
        return result;
    }
}
